package com.ruoyi.service.impl;


import com.ruoyi.domain.MineLtcdjc;
import com.ruoyi.domain.SysCoalMachine;
import com.ruoyi.domain.dto.SysCoalMachineDto;
import com.ruoyi.enums.BasicInformationEnum;
import com.ruoyi.mapper.MineLtcdjcMapper;
import com.ruoyi.mapper.SysCoalMachineMapper;
import com.ruoyi.service.MineLtcdjcService;
import com.ruoyi.service.SlopeMonitorService;
import com.ruoyi.utils.MeasuringPointValueUtils;
import com.ruoyi.utils.SnowflakeIdUtils;
import com.ruoyi.utils.WeiteTxtFileUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


@Service
public class SlopeMonitorServiceImpl implements SlopeMonitorService {
    private static String[] IGNORE_ISOLATOR_PROPERTIES = new String[]{"detectedTime"};

    @Autowired
    private WeiteTxtFileUtils weiteTxtFileUtils;
    @Autowired
    private SysCoalMachineMapper sysCoalMachineMapper;
    @Autowired
    private MineLtcdjcMapper mineLtcdjcMapper;
    @Autowired
    private MineLtcdjcService mineLtcdjcService;

    @Override
    public void slopeMonitorcreateTxt() {
        Boolean malfunction = false;
        List<SysCoalMachine> fileBody = sysCoalMachineMapper.slopeMonitorcreateTxt();
        for (SysCoalMachine sysCoalMachine : fileBody) {
sysCoalMachine.setMalfunction((byte) 0);
            Integer judge = sysCoalMachine.getJudge();
            if (judge == 1) {
                sysCoalMachine.setMalfunction((byte) 1);
                mineLtcdjcMapper.updateMineLtcdjc(sysCoalMachine);
                sysCoalMachine.setStationState(BasicInformationEnum.POWER_FAILURE_FAULT.getDesc());
                malfunction = true;
            }else if (judge == 2) {
                sysCoalMachine.setMalfunction((byte) 1);
                mineLtcdjcMapper.updateMineLtcdjc(sysCoalMachine);
                sysCoalMachine.setStationState(BasicInformationEnum.EQUIPMENT_FAILURE.getDesc());
                malfunction = true;
            }else if (judge == 0) {
                mineLtcdjcMapper.updateMineLtcdjc(sysCoalMachine);
            }
        }

        txtMethod(fileBody);
        if (malfunction){
            mineLtcdjcService.mineLtcdjcCreateTxt();
            System.out.println("执行基本信息生成txt");
        }
    }

    //
    @Override
    public Boolean insertMachine(SysCoalMachineDto sysCoalMachineDto) {
        try {
            SysCoalMachine sysCoalMachine = new SysCoalMachine();
            BeanUtils.copyProperties(sysCoalMachineDto, sysCoalMachine);
            sysCoalMachine.setMachineId(new SnowflakeIdUtils(10,10).nextId());
            sysCoalMachine.setCreateTime(LocalDateTime.now());
            MeasuringPointValueUtils.eastValue(sysCoalMachine);
            sysCoalMachineMapper.insertCoalData(sysCoalMachine);
            //插入成功返回主键id开始生成txt文件
            //todo 生成txt文件sysCoalMachine使用这个对象里面参数
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Boolean deleteMachine() {

        LocalDateTime createTime = LocalDateTime.now().plusMonths(-1);
        sysCoalMachineMapper.deleteMachine(createTime);


        return true;
    }

    @Override
    public Boolean updateMachine(SysCoalMachineDto machineSwitchDto) {
        return null;
    }


    public void txtMethod(List<SysCoalMachine> fileBody) {
        String fileNameDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String fileHeaderDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String fileName = BasicInformationEnum.FILE_BASIC_INFO.getDesc() + BasicInformationEnum.REAL_FILE_TIME.getDesc()
                + fileNameDate
                + ".txt";
        String fileHeader = BasicInformationEnum.FILE_HEADER.getDesc() + fileHeaderDate ;


        weiteTxtFileUtils.urlVerifyAndAdd(BasicInformationEnum.FILE_BASIC_INFO.getDesc(), fileName, fileHeader, fileBody);


    }
}
